package com.Generics.bean.ch05;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author nmm
 * @date 2024-03-2024/3/31
 * @deriction njm......
 */
public class Woman <T extends Animal>{

    private T pet;

    private String name;

    public void setPet(T pet) {
        this.pet = pet;
    }

    public void setName(String name) {
        this.name = name;
    }


    public Woman(T pet, String name) {
        this.pet = pet;
        this.name = name;
    }

    public void sayHello() {
        try {
            Class<?> myPetClazz = pet.getClass();//获取class实例

            Field petFieldName = myPetClazz.getDeclaredField("name");//拿到完整的属性(private java.lang.String com.Generics.annotation.ch01.Dog.pname)
            petFieldName.setAccessible(true);//凡是对公开以外的属性要进行操作时,都要开启可访问为true

            Object petName = petFieldName.get(pet);//通过具体的实例拿到其具体属性值
            System.out.println(String.format("%s对%s打招呼了,\\(@^0^@)/",name,petName));

            System.out.println(petName+"回应到:");
            Method soundMethod = myPetClazz.getDeclaredMethod("sound");
            soundMethod.invoke(pet);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}